import {createSlice,createAsyncThunk} from "@reduxjs/toolkit"
import axios from "axios"

export let getRouteThunk = createAsyncThunk('getRouteThunk',async(id)=>{
    let res = await axios.get("http://172.19.200.4:8001/getroute?id="+id)
    localStorage.setItem("userroutes",JSON.stringify(res.data.data))
    return res.data.data
})

let _userRoutes = localStorage.getItem("userroutes")

let userSlice = createSlice({
    name:"userSlice",
    initialState:{
        userRoutes:_userRoutes ? JSON.parse(_userRoutes) : []
    },
    extraReducers:{
        [getRouteThunk.fulfilled]:(state,action)=>{
            state.userRoutes = action.payload
        }   
    }
})

export default userSlice.reducer